<template>
    <view>
        <view class="head" style="background: #fff">
            <view class="date-display flex" :style="'background:' + color">
                <view class="next-prev">
                    <view @tap="changeMonth" class="arrow prev" id="prev"></view>
                </view>
                <view class="selected-date">{{ currYear }}年{{ currMonth }}月</view>
                <view class="next-prev">
                    <view @tap="changeMonth" class="arrow next" id="next"></view>
                </view>
            </view>
        </view>
        <view class="container-rl">
            <view class="lxqd">
                连续签到
                <text :style="'color:' + color">{{ lxts }}</text>
                天了
            </view>
            <view class="week-display qdflex">
                <view :class="'week-cell ' + item.class" v-for="(item, index) in week" :key="index">{{ item.value }}</view>
            </view>
            <view class="date-picker">
                <view class="qdflex">
                    <view class="date-cell" v-for="(item, index) in emptyGrids" :key="index"></view>
                    <view class="date-cell" :data-selected="[currYear, currMonth, item.value]" v-for="(item, index) in days" :key="index">
                        <view :data-selected="[currYear, currMonth, item.value]" v-if="item.isqd != null">
                            <icon :color="color" size="27" type="success"></icon>
                        </view>

                        <view
                            @tap="selectDate"
                            :class="item.class + (selected == item.value ? 'selected' : '') + ' ' + (xz == item.value ? 'xz' : '')"
                            :data-selected="[currYear, currMonth, item.value]"
                            :data-tsrq="days[index]"
                            :style="'background:' + item.tsrq.color + ';color:#fff;'"
                            v-if="item.isqd == null && item.tsrq != null"
                        >
                            {{ item.value }}
                        </view>

                        <view
                            @tap="selectDate"
                            :class="item.class + (selected == item.value ? 'selected' : '') + ' ' + (xz == item.value ? 'xz' : '')"
                            :data-selected="[currYear, currMonth, item.value]"
                            :data-tsrq="days[index]"
                            v-if="item.isqd == null && item.tsrq == null"
                        >
                            {{ item.value }}
                        </view>
                    </view>
                </view>
            </view>
            <view class="xxts">
                日常签到
                <text :style="'color:' + color">+{{ qdset[0].integral }}</text>
                积分,有背景色的日期含有特殊奖励哦~
            </view>
            <view class="weui-btn-area">
                <button
                    @tap="qd"
                    class="weui-btn"
                    :disabled="disabled"
                    :style="'background:' + (disabled ? '#d5d5d5' : color) + ';'"
                    type="primary"
                    v-if="!isbq || qdset[0].is_bq == '2'"
                >
                    {{ logintext }}
                </button>
                <button @tap="bq" class="weui-btn" :disabled="bqdisabled" :style="'background:' + (bqdisabled ? '#d5d5d5' : color) + ';'" type="primary" v-else>
                    {{ bqtext }}
                </button>
            </view>
        </view>
        <navigator url="pm">
            <view class="ckpm" :style="'color:' + color">查看排名></view>
        </navigator>
        <view style="background: #fff">
            <view class="jfgz1">
                <image class="wjx" src="https://ymqycc.oss-cn-chengdu.aliyuncs.com/ymqzho2o/static/ymq_canyino2o/images/wjx.png"></image>
                <view>首次签到</view>
            </view>
            <view class="jfgz2">
                <view class="jlgz">
                    <text>首次签到可额外获得{{ qdset[0].one }}积分</text>
                </view>
            </view>
            <view class="jfgz1">
                <image class="wjx" src="https://ymqycc.oss-cn-chengdu.aliyuncs.com/ymqzho2o/static/ymq_canyino2o/images/wjx.png"></image>
                <view>奖励规则</view>
            </view>
            <view class="jfgz2">
                <view class="jlgz">
                    <text v-for="(item, index) in jl" :key="index">{{ index + 1 }}.连续签到{{ item.day }}天赠送{{ item.integral }}积分</text>
                </view>
                <image class="jfjl" mode="aspectFill" :src="app_url + qdset[0].qd_img"></image>
            </view>
        </view>
    </view>
</template>

<script>
import cardlist from '@/ymq_canyino2o/dist/cardlist/index';
import gettel from '@/ymq_canyino2o/dist/gettel/index';
var app = getApp();
var util = require('../../utils/util.js');
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth() + 1;
var dayInMonth = date.getDate();
var dayInWeek = date.getDay();
var selected = [year, month, dayInMonth];
var week = [
    {
        value: '日',
        class: 'weekend'
    },
    {
        value: '一',
        class: ''
    },
    {
        value: '二',
        class: ''
    },
    {
        value: '三',
        class: ''
    },
    {
        value: '四',
        class: ''
    },
    {
        value: '五',
        class: ''
    },
    {
        value: '六',
        class: 'weekend'
    }
];
var isLeapYear = function (t) {
    return t % 400 == 0 || (t % 4 == 0 && t % 100 != 0);
};
var isToday = function (t, e, a) {
    return t == year && e == month && a == dayInMonth;
};
var isWeekend = function (t, e) {
    return (t + e) % 7 == 0 || (t + e - 1) % 7 == 0;
};
var calEmptyGrid = function (t, e) {
    return new Date(t + '/' + e + '/02 00:00:00').getUTCDay();
};
var calDaysInMonth = function (t, e) {
    var a = isLeapYear(t);
    return 2 == month && a ? 29 : 2 != month || a ? ([4, 6, 9, 11].includes(e) ? 30 : 31) : 28;
};
var calWeekDay = function (t, e, a) {
    return new Date(t + '/' + e + '/' + a + ' 00:00:00').getUTCDay();
};
var getThisMonthDays = function (t, e) {
    return new Date(t, e, 0).getDate();
};
var calDays = function (t, e) {
    for (var a = getThisMonthDays(t, e), s = calEmptyGrid(t, e), n = [], o = 1; o <= a; o++) {
        var l = isToday(t, e, o);
        var i = selected[0] == t && selected[1] == e && selected[2] == o;
        var r = l ? 'today' : '';
        var d = i ? 'selected' : '';
        var c = {
            value: o,
            date: [t, e, o],
            class: 'date-bg ' + (isWeekend(s, o) ? 'weekend' : '') + ' ' + r + ' ' + d + ' ' + (l && i ? 'today-selected' : '')
        };
        n.push(c);
    }
    return n.slice(0, calDaysInMonth(t, e));
};
export default {
    components: {
        cardlist,
        gettel
    },
    data() {
        return {
            currYear: year,
            currMonth: month,
            week: week,
            emptyGrids: calEmptyGrid(year, month),
            days: calDays(year, month),
            selected: selected,
            disabled: false,
            logintext: '点击签到',
            lxts: 0,
            isbq: false,
            bqtext: '点击补签',
            nowtime: '',
            qdset: '',
            jl: '',
            havebq: '',
            grjf: '',
            userinfo: '',
            wdqd: '',
            special: '',
            bqtime: '',
            bqdisabled: false,
            xz: '',
            color: '',
            integral: '',
            is_bq: '',
            one: '',
            app_url: '',
            qd_img: ''
        };
    },
    onLoad: function () {
        function t() {
            var t = new Date();
            var e = t.getMonth() + 1;
            var a = t.getDate();
            return [t.getFullYear(), e, a];
        }
        app.globalData.setNavigationBarColor(this);
        console.log(this.days, this.selected);
        console.log(t());
        this.setData({
            nowtime: t()
        });
        var that = this;
        app.globalData.util.request({
            url: 'entry/wxapp/Signset',
            cachetime: '0',
            success: function (t) {
                console.log('签到设置', t);
                that.setData({
                    qdset: t.data
                });
            }
        });
        app.globalData.util.request({
            url: 'entry/wxapp/ContinuousList',
            cachetime: '0',
            success: function (t) {
                console.log('查看连签奖励', t);
                that.setData({
                    jl: t.data
                });
            }
        });
        this.reLoad();
        this.lqts();
    },
    onPullDownRefresh: function () {
        this.reLoad();
        this.lqts();
        setTimeout(function () {
            uni.stopPullDownRefresh();
        }, 1000);
    },
    methods: {
        in_array: function (t, e) {
            for (var a = 0; a < e.length; a++) {
                if (e[a].toString() == t) {
                    return true;
                }
            }
            return false;
        },

        lqts: function () {
            this.setData({
                isbq: false
            });
            var that = this;
            var t = uni.getStorageSync('users').id;
            app.globalData.util.request({
                url: 'entry/wxapp/Continuous',
                cachetime: '0',
                data: {
                    user_id: t
                },
                success: function (t) {
                    console.log('查看连续签到天数', t);
                    that.setData({
                        lxts: t.data
                    });
                }
            });
            app.globalData.util.request({
                url: 'entry/wxapp/Isbq',
                cachetime: '0',
                data: {
                    user_id: t
                },
                success: function (t) {
                    console.log('isbq', t);
                    that.setData({
                        havebq: t.data
                    });
                }
            });
            app.globalData.util.request({
                url: 'entry/wxapp/userinfo',
                cachetime: '0',
                data: {
                    user_id: t
                },
                success: function (t) {
                    console.log('个人信息', t);
                    that.setData({
                        grjf: t.data.total_score,
                        userinfo: t.data
                    });
                }
            });
        },

        reLoad: function () {
            uni.showLoading({
                title: '加载中',
                mask: true
            });
            var that = this;
            var t = uni.getStorageSync('users').id;
            app.globalData.util.request({
                url: 'entry/wxapp/MySign',
                cachetime: '0',
                data: {
                    user_id: t
                },
                success: function (t) {
                    console.log('我的签到', t);
                    that.setData({
                        wdqd: t.data
                    });
                    for (var e = [], d = that.days, a = 0; a < t.data.length; a++) {
                        e.push(t.data[a].time);
                    }
                    console.log(e, d);
                    if (that.in_array(that.nowtime.toString(), e)) {
                        console.log('今日已签到');
                        that.setData({
                            disabled: true,
                            logintext: '今日已签到'
                        });
                    } else {
                        console.log('今日未签到');
                        that.setData({
                            disabled: false,
                            logintext: '点击签到'
                        });
                    }
                    for (var s = 0; s < d.length; s++) {
                        if (that.in_array(d[s].date.toString(), e)) {
                            d[s].isqd = 1;
                        }
                    }
                    app.globalData.util.request({
                        url: 'entry/wxapp/Special',
                        cachetime: '0',
                        success: function (t) {
                            console.log('Special', t);
                            for (var e = t.data, a = 0; a < e.length; a++) {
                                e[a].day = e[a].day.split('-');
                                var s = new Date(e[a].day[0], e[a].day[1] - 1, e[a].day[2]);
                                var n = s.getFullYear();
                                var o = s.getMonth() + 1;
                                var l = s.getDate();
                                e[a].day = n + ',' + o + ',' + l;
                            }
                            console.log(e);
                            that.setData({
                                special: e
                            });
                            for (var i = 0; i < d.length; i++) {
                                for (var r = 0; r < e.length; r++) {
                                    if (d[i].date.toString() == e[r].day) {
                                        d[i].tsrq = e[r];
                                    }
                                }
                            }
                            that.setData({
                                days: d
                            });
                        }
                    });
                }
            });
        },

        qd: function () {
            console.log("执行签到")
            var that = this;
            var t = this.userinfo;
            console.log(t);
            if ('' != t.img && '' != t.name) {
                var a = uni.getStorageSync('users').id;
                var s = this.wdqd;
                console.log(that.nowtime, that.special, that.qdset, s);
                for (var n = that.qdset[0].integral, o = 0; o < that.special.length; o++) {
                    if (that.nowtime.toString() == that.special[o].day) {
                        n = that.special[o].integral;
                    }
                }
                if (0 == s.length) {
                    var l = that.qdset[0].one;
                } else {
                    l = 0;
                }
                console.log(n, l);
                uni.showLoading({
                    title: '加载中',
                    mask: true
                });
                app.globalData.util.request({
                    url: 'entry/wxapp/Sign',
                    cachetime: '0',
                    data: {
                        user_id: a,
                        time: that.nowtime.toString(),
                        integral: n,
                        one: l
                    },
                    success: function (t) {
                        uni.showLoading({
                            title: '签到成功',
                            mask: true
                        });
                        console.log(t);
                        that.reLoad();
                        that.lqts();
                    }
                });
            } else {
                uni.navigateTo({
                    url: '/ymq_canyino2o/pages/my/userinfo' // ../smdc/getdl
                });
            }
        },

        bq: function () {
            var that = this;
            var t = this.userinfo;
            console.log(t);
            if ('' != t.img && '' != t.name) {
                var a = uni.getStorageSync('users').id;
                var s = this.wdqd;
                var n = Number(this.grjf);
                console.log(that.bqtime, that.special, that.qdset, s, n);
                for (var o = that.qdset[0].integral, l = 0; l < that.special.length; l++) {
                    if (that.bqtime.toString() == that.special[l].day) {
                        o = that.special[l].integral;
                    }
                }
                if (0 == s.length) {
                    var i = that.qdset[0].one;
                } else {
                    i = 0;
                }
                console.log(o, i);
                uni.showModal({
                    title: '温馨提示',
                    content: '补签将会扣除您' + that.qdset[0].bq_integral + '积分哦',
                    success: function (t) {
                        if (t.confirm) {
                            console.log('用户点击确定');
                            if (Number(that.qdset[0].bq_integral) > n) {
                                uni.showModal({
                                    title: '提示',
                                    content: '您的积分为' + n + ',不足补签扣除'
                                });
                            } else {
                                uni.showLoading({
                                    title: '加载中',
                                    mask: true
                                }),
                                    app.globalData.util.request({
                                        url: 'entry/wxapp/Sign2',
                                        cachetime: '0',
                                        data: {
                                            user_id: a,
                                            time: that.bqtime.toString(),
                                            integral: o,
                                            one: i
                                        },
                                        success: function (t) {
                                            console.log(t);
                                            that.reLoad();
                                            that.lqts();
                                        }
                                    });
                            }
                        } else {
                            if (t.cancel) {
                                console.log('用户点击取消');
                            }
                        }
                    }
                });
            } else {
                uni.navigateTo({
                    url: '/ymq_canyino2o/pages/my/userinfo' // ../smdc/getdl
                });
            }
        },

        changeMonth: function (t) {
            var e = t.target.id;
            var a = this.currYear;
            var s = this.currMonth;
            if ('prev' == e) {
                s = s - 1;
            } else {
                s = s + 1;
            }
            if ('prev' == e && s < 1) {
                a -= 1;
                s = 12;
            }
            if ('next' == e && 12 < s) {
                a += 1;
                s = 1;
            }
            this.setData({
                currYear: a,
                currMonth: s,
                emptyGrids: calEmptyGrid(a, s),
                days: calDays(a, s)
            });
            this.reLoad();
        },

        selectDate: function (t) {
            var that = this;
            var a = this.havebq;
            var s = that.nowtime;
            var n = t.currentTarget.dataset.selected;
            var o = t.currentTarget.dataset.tsrq;
            console.log(s, n, o);
            that.setData({
                bqtime: n
            });
            var l = new Date(s[0], s[1], s[2]);
            var i = new Date(n[0], n[1], n[2]);
            var r = l.getTime();
            var d = i.getTime();
            console.log(r, d, a);
            if (d < r) {
                console.log('以前');
                if (2 == a) {
                    that.setData({
                        bqdisabled: false,
                        bqtext: '点击补签'
                    });
                } else {
                    that.setData({
                        bqdisabled: true,
                        bqtext: '今日已补签一次'
                    });
                }
                that.setData({
                    isbq: true
                });
                if (null != o.tsrq) {
                    uni.showModal({
                        title: o.tsrq.day + '是' + o.tsrq.title,
                        content: '本日签到特殊奖励' + o.tsrq.integral + '积分'
                    });
                }
            } else {
                if (r == d) {
                    null != o.tsrq &&
                        uni.showModal({
                            title: o.tsrq.day + '是' + o.tsrq.title,
                            content: '本日签到特殊奖励' + o.tsrq.integral + '积分'
                        }),
                        console.log('今日');
                } else {
                    null != o.tsrq &&
                        uni.showModal({
                            title: o.tsrq.day + '是' + o.tsrq.title,
                            content: '本日签到特殊奖励' + o.tsrq.integral + '积分'
                        }),
                        console.log('以后');
                }
                that.setData({
                    isbq: false
                });
            }
            var c = o.value;
            this.setData({
                xz: c
            });
        }
    }
};
</script>
<style>
@import './qd.css';
</style>
